package com.example.SocialApplication.adapters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.SocialApplication.R;
import com.example.SocialApplication.activity.lists.AddNewListActivity;
import com.example.SocialApplication.db.entity.ListThings;

import java.io.File;
import java.util.List;

public class ListAdapter extends ArrayAdapter {
    private List<ListThings> mList;
    private Context mContext;

    public ListAdapter(Context mContext, int textViewResourceId, List<ListThings> objects) {
        super(mContext, textViewResourceId, objects);
        this.mList = objects;
        this.mContext = mContext;
    }

    @Override
    public ListThings getItem(int position) {
        return mList.get(position);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = ((Activity) mContext).getLayoutInflater().inflate(R.layout.inflater_list_item, parent, false);
        }

        if (getItem(position).getName().equals("ADD")) {
            view = ((Activity) mContext).getLayoutInflater().inflate(R.layout.new_list_item, parent, false);
            TextView text = (TextView) view.findViewById(R.id.name);
            text.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
            text.setText("Добавить новый");
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, AddNewListActivity.class);
                    mContext.startActivity(intent);
                }
            });
        } else {
            view = ((Activity) mContext).getLayoutInflater().inflate(R.layout.inflater_list_item, parent, false);
            TextView text = (TextView) view.findViewById(R.id.inflater_list_item_name);
            text.setText(mList.get(position).getName());
            LinearLayout layout = (LinearLayout) view.findViewById(R.id.inflater_list_item_layout);
            layout.setBackgroundColor(Color.parseColor(getItem(position).getColor().getBackground()));
            ImageView image = (ImageView) view.findViewById(R.id.inflater_list_item_image);
            image.setImageBitmap(getBitmap(getItem(position).getPictureId()));
            //text.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
//            view.setBackgroundColor(Color.rgb(35, 113, 188));

        }

//        if (getItem(position).toString().equals("ADD")) {
//
//        } else {
//
//        }
        return view;
    }

    private Bitmap getBitmap(String name) {
        String pathToFolder = mContext.getExternalCacheDir().getAbsolutePath()
                + File.separator
                + "images"
                + File.separator;

        String fileName = File.separator + name + ".png";
        String path = pathToFolder + fileName;

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap result = BitmapFactory.decodeFile(path, options);
        return result;
    }
}
